const CourseCollectionModel = require('../db/models/courseCollection');
//这里的courseCollection就是course

class CourseCollectionServer {
  async addCourseCollectionData (data) {
    //检查cid是否存在 check cid is existed
    const cid = data.cid;
    const result = await CourseCollectionModel.findOne({where: { cid }});
    //判断 check
    if (result) {
      //更新 update
      return await CourseCollectionModel.update(data, {where: { cid }})
    }else {
      //创建 create
      return await CourseCollectionModel.create(data);
    }
  }


  async getCourseCollectionData () {
    const data =  await CourseCollectionModel.findAll({
      attributes: {
        exclude: ['posterUrl', 'createdAt', 'updatedAt']
      }  
    });
    return data;
  }


  async changeCourseCollectionStatus (id, status) {
     //公用中间件控制器
     const result =  await CourseCollectionModel.update({ status }, {
      where: { id }
    })
    return result[0];
  }
}

module.exports = new CourseCollectionServer();